package com.example.gramediamajalahmobile.main;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.example.gramediamajalahmobile.JSONParser;
import com.example.gramediamajalahmobile.R;
import com.example.gramediamajalahmobile.channel.Channel;
import com.example.gramediamajalahmobile.search.Search;

public class Article extends Activity {

	// All static variables
	//static String link_url = "http://192.168.2.30/codeigniter-restserver-master/index.php/gom/articles";
	static final String AR_ID = "id";
	static final String AR_IMAGE = "image_filename";
	static final String AR_JUDUL = "title";
	static final String AR_SHORDESC = "short_description";
	ListView list;
	LazyAdapter adapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listview);
		Bundle b = getIntent().getExtras();
		int channel = (int) b.getLong(Channel.AR_CHANNEL);
		 String link_url = "http://192.168.2.23/codeigniter-restserver-master/index.php/gom/articles?channel="+channel ;
		ArrayList<HashMap<String, String>> daftar_article = new ArrayList<HashMap<String, String>>();

		

		// Creating JSON Parser instance
		JSONParser jParser = new JSONParser();
		JSONArray json = jParser.AmbilJson(link_url);

		try {
			for (int i = 0; i < json.length(); i++) {
				JSONObject ar = json.getJSONObject(i);

				String id = ar.getString(AR_ID);
				String title = ar.getString(AR_JUDUL);
				String shortdesc = ar.getString(AR_SHORDESC);
				String image = ar.getString(AR_IMAGE);

				HashMap<String, String> map = new HashMap<String, String>();

				map.put(AR_ID, id);
				map.put(AR_JUDUL, title);
				map.put(AR_SHORDESC, shortdesc);
				map.put(AR_IMAGE, image);

				daftar_article.add(map);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		ImageButton search = (ImageButton) findViewById(R.id.cari);
		search.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent in = new Intent(Article.this,Search.class);
				startActivity(in);
				
			}
		});
		list = (ListView) findViewById(R.id.list);

		LazyAdapter adapter = new LazyAdapter(this, daftar_article);
		list.setAdapter(adapter);
		// Getting adapter by passing json data ArrayList

		// Click event for single list row
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				String kode = ((TextView) view.findViewById(R.id.id)).getText()
						.toString();

				Intent in = new Intent(Article.this, Detail.class);
				in.putExtra(AR_ID, kode);
				startActivity(in);

			}
		});
	}
}